package com.itboye.ihomebank.home;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.JinJiRenOtherHouseAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.HouseDetailBean;
import com.itboye.ihomebank.bean.HouseDetailBean.OwnerInfo.OtherHouses;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.homefragment.HouseDetailActivity;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.XImageLoader;

public class ActivityJinJiRenFangYuan extends BaseOtherActivity {
	XListView xlistView;
	TextView add_shap_title_tv;
	ImageView close_icon;
	HouseDetailBean houseDetailBean;
	RoundImageView head;
	TextView name;
	ImageView message, phone;
	private List<OtherHouses> otherHouse;
	JinJiRenOtherHouseAdapter adapter;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.phone:
			intent = new Intent(Intent.ACTION_DIAL,
					Uri.parse("tel:"+houseDetailBean.getOwnerInfo().getMobile()));
			System.out.println(houseDetailBean.getOwnerInfo().getMobile()+">>>>");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			break;
		case R.id.message://聊天界面
			//此实现不一定要放在Application onCreate中
			final String userid = "tbimuser_8f85517967795eeef66c225f7883bdcb";
			//此对象获取到后，保存为全局对象，供APP使用
			//此对象跟用户相关，如果切换了用户，需要重新获取
			final YWIMKit mIMKit = YWAPI.getIMKitInstance(userid, SPContants.YW_APPKEY);
			//开始登录
			String password = "itboye";
			IYWLoginService loginService = mIMKit.getLoginService();
			YWLoginParam loginParam = YWLoginParam.createLoginParam(userid, password);
			loginService.login(loginParam, new IWxCallback() {
			 
			    @Override
			    public void onSuccess(Object... arg0) {
			    	ByAlert.alert(arg0);
			    	
			    	final String target = houseDetailBean.getOwnerInfo().getBaichuanUid(); //消息接收者ID
			    	final String appkey = SPContants.YW_APPKEY; //消息接收者appKey
			    	Intent intent = mIMKit.getChattingActivityIntent(target, appkey);
			    	startActivity(intent);
//			    	
//			    	Intent intent = mIMKit.getConversationActivityIntent();
//			    	startActivity(intent);
			    }
			 
			    @Override
			    public void onProgress(int arg0) {
			        // TODO Auto-generated method stub
			    	System.out.println(arg0+">>yw");
			    }
			 
			    @Override
			    public void onError(int errCode, String description) {
			    	ByAlert.alert(errCode+":"+description);//如果登录失败，errCode为错误码,description是错误的具体描述信息
			    }
			});
			break;
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_jingjirenfangyuan;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		xlistView.setPullLoadEnable(false);
		xlistView.setPullRefreshEnable(false);
		houseDetailBean = (HouseDetailBean) getIntent().getSerializableExtra(
				"model");
		add_shap_title_tv.setText(getIntent().getStringExtra(
				houseDetailBean.getOwnerInfo().getName()));
		name.setText(houseDetailBean.getOwnerInfo().getName());
		XImageLoader.load(houseDetailBean.getOwnerInfo().getHead(), head);
		otherHouse = houseDetailBean.getOwnerInfo().getOtherHouses();
		adapter = new JinJiRenOtherHouseAdapter(this, otherHouse,
				R.layout.item_otherhouse);
		xlistView.setAdapter(adapter);
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String uid=SPUtils.get(ActivityJinJiRenFangYuan.this, null, SPContants.USER_ID, "")+"";
				Intent intent=new Intent(ActivityJinJiRenFangYuan.this,HouseDetailActivity.class);
				intent.putExtra("uid", uid);
				intent.putExtra("house_no", houseDetailBean.getOwnerInfo().getOtherHouses().get(position-1).getHouseNo());
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

}
